package com.carsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.carsystem.entity.Car;
import com.carsystem.entity.CarBrandAndCar;
import com.carsystem.entity.Carbrand;
import com.carsystem.entity.CarbrandCar;
import com.carsystem.mapper.CarbrandCarMapper;
import com.carsystem.mapper.CarbrandMapper;
import com.carsystem.service.ICarbrandService;
import com.carsystem.utils.MessageConstant;
import com.carsystem.utils.PageResult;
import com.carsystem.utils.QueryPageBean;
import com.carsystem.utils.Result;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ykl
 * @since 2021-12-04
 */
@Service
public class CarbrandServiceImpl extends ServiceImpl<CarbrandMapper, Carbrand> implements ICarbrandService {

    @Resource
    private CarbrandMapper carbrandMapper;
    @Resource
    private CarbrandCarMapper carbrandCarMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page<Carbrand> carbrandPage = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<Carbrand> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(queryPageBean.getQueryString())){
            queryWrapper.like("name",queryPageBean.getQueryString());
        }
        Page<Carbrand> page = carbrandMapper.selectPage(carbrandPage, queryWrapper);
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return pageResult;
    }

    @Override
    public Result saveCarBrand(Carbrand carbrand, int[] carIds) {
        //添加carbrand
        int row = carbrandMapper.insert(carbrand);
        Integer id = carbrand.getId();
        if (carIds!=null && carIds.length>0){
            for (int carId : carIds) {
                CarbrandCar carbrandCar = new CarbrandCar();
                carbrandCar.setCarId(carId);
                carbrandCar.setCarbrandId(id);
                carbrandCarMapper.insert(carbrandCar);
            }
        }
        if (row==1){
            return new Result(true, MessageConstant.ADD_CARBRAND_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_CARBRAND_FAIL);
    }

    @Override
    public Result deleteCarBrand(int id) {
        //先删除中间表
        QueryWrapper<CarbrandCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("carbrand_id",id);
        carbrandCarMapper.delete(queryWrapper);

        int row = carbrandMapper.deleteById(id);
        if (row==1){
            return new Result(true, MessageConstant.DELETE_CARBRAND_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_CARBRAND_FAIL);
    }

    @Override
    public Result updateCarBrand(Carbrand carbrand, int[] carIds) {
        int row = carbrandMapper.updateById(carbrand);
        //查询这个汽车品牌有什么汽车
        QueryWrapper<CarbrandCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("carbrand_id",carbrand.getId());
        carbrandCarMapper.delete(queryWrapper);
        if (carIds!=null && carIds.length>0){
            for (int carId : carIds) {
                CarbrandCar carbrandCar = new CarbrandCar();
                carbrandCar.setCarId(carId);
                carbrandCar.setCarbrandId(carbrand.getId());
                carbrandCarMapper.insert(carbrandCar);
            }
        }
        if (row==1){
            return new Result(true, MessageConstant.EDIT_CARBRAND_SUCCESS);
        }
        return new Result(false, MessageConstant.EDIT_CARBRAND_FAIL);
    }

    @Override
    public CarBrandAndCar getCarBrandAndCarById(int id) {
        Carbrand carbrand = carbrandMapper.selectById(id);
        //汽车品牌有什么汽车
        QueryWrapper<CarbrandCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("carbrand_id",id);
        List<CarbrandCar> carbrandCars = carbrandCarMapper.selectList(queryWrapper);
        ArrayList<Integer>  ids = new ArrayList<>();
        for (CarbrandCar carbrandCar : carbrandCars) {
            ids.add(carbrandCar.getCarId());
        }
        return new CarBrandAndCar(carbrand,ids);
    }

    @Override
    public Result findAllCarbrand() {
        List<Carbrand> carbrands=null;
        try {
            carbrands = carbrandMapper.selectList(null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CARBRAND_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_CARBRAND_SUCCESS,carbrands);
    }
}
